package org.example.springboot.service.impl;

import com.xxl.job.core.biz.model.XxlJobInfo;
import lombok.AllArgsConstructor;
import org.example.springboot.common.config.XxlJobConfig;
import org.example.springboot.common.service.impl.BaseServiceImpl;
import org.example.springboot.common.utils.XxlJobUtil;
import org.example.springboot.dao.DecisionTimeDao;
import org.example.springboot.entity.DecisionTime;
import org.example.springboot.service.DecisionTimeService;
import org.example.springboot.utils.QuartzCronExpressionGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Fz
 * @since 1.0.0 2024-07-31
 */
@Service
@AllArgsConstructor
public class DecisionTimeServiceImpl extends BaseServiceImpl<DecisionTimeDao, DecisionTime> implements DecisionTimeService {
    @Resource
    private final XxlJobConfig xxlJobConfig;

    @Override
    public void decisionTimeSet(DecisionTime decisionTime) {
        String adminAddresses = xxlJobConfig.getAdminAddresses();
        XxlJobUtil xxlJobUtil = new XxlJobUtil();
        this.saveOrUpdate(decisionTime);
        //删除之前的定时任务
        String jobDesc = "ET决策任务,水分控制决策任务,轮灌决策任务";
        String[] descArr = jobDesc.split(",");
        for (String desc : descArr) {
            xxlJobUtil.removeByCondition(adminAddresses, desc);
        }
        createEtCheckHandler(decisionTime.getDecisionTime());
        moistureCheckHandler(decisionTime.getDecisionTime());
        inTurnCheckHandler(decisionTime.getDecisionTime());
    }

    private void inTurnCheckHandler(String decisionTime){
        try{
            //执行器，任务Handler名称
            XxlJobInfo jobInfo = new XxlJobInfo();
            String handler = "createInTurnCheckHandler";
            String jobDesc = "轮灌决策任务";
            jobInfo.setAuthor("admin");
            jobInfo.setJobDesc(jobDesc);
            jobInfo.setExecutorHandler(handler);
            jobInfo.setExecutorParam(decisionTime);
            jobInfo.setScheduleType("CRON");
            // 定义时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

            // 解析时间字符串为LocalTime对象
            LocalTime localTime = LocalTime.parse(decisionTime, formatter);

            // 增加1分钟
            LocalTime incrementedTime = localTime.plusMinutes(1);

            // 格式化回字符串
            String incrementedTimeString = incrementedTime.format(formatter);
            jobInfo.setScheduleConf(QuartzCronExpressionGenerator.generateQuartzCronExpression(incrementedTimeString));
            jobInfo.setGlueType("BEAN");
            jobInfo.setExecutorRouteStrategy("FIRST");
            jobInfo.setMisfireStrategy("DO_NOTHING");
            jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
            //创建定时任务提醒用户
            XxlJobUtil xxlJobUtil = new XxlJobUtil();
            xxlJobUtil.addAndStartTask("xxl-job-irrigation", xxlJobConfig.getAdminAddresses(), jobInfo);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

     private void createEtCheckHandler(String decisionTime){
        try{
            //执行器，任务Handler名称
            XxlJobInfo jobInfo = new XxlJobInfo();
            String handler = "createEtCheckHandler";
            String jobDesc = "ET决策任务";
            jobInfo.setAuthor("admin");
            jobInfo.setJobDesc(jobDesc);
            jobInfo.setExecutorHandler(handler);
            jobInfo.setExecutorParam(decisionTime);
            jobInfo.setScheduleType("CRON");
            jobInfo.setScheduleConf(QuartzCronExpressionGenerator.generateQuartzCronExpression(decisionTime));
            jobInfo.setGlueType("BEAN");
            jobInfo.setExecutorRouteStrategy("FIRST");
            jobInfo.setMisfireStrategy("DO_NOTHING");
            jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
            //创建定时任务提醒用户
            XxlJobUtil xxlJobUtil = new XxlJobUtil();
            xxlJobUtil.addAndStartTask("xxl-job-irrigation", xxlJobConfig.getAdminAddresses(), jobInfo);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void moistureCheckHandler(String decisionTime){
        try{
            //执行器，任务Handler名称
            XxlJobInfo jobInfo = new XxlJobInfo();
            String handler = "moistureCheckHandler";
            String jobDesc = "水分控制决策任务";
            jobInfo.setAuthor("admin");
            jobInfo.setJobDesc(jobDesc);
            jobInfo.setExecutorHandler(handler);
            jobInfo.setExecutorParam(decisionTime);
            jobInfo.setScheduleType("CRON");
            jobInfo.setScheduleConf(QuartzCronExpressionGenerator.generateQuartzCronExpression(decisionTime));
            jobInfo.setGlueType("BEAN");
            jobInfo.setExecutorRouteStrategy("FIRST");
            jobInfo.setMisfireStrategy("DO_NOTHING");
            jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
            //创建定时任务提醒用户
            XxlJobUtil xxlJobUtil = new XxlJobUtil();
            xxlJobUtil.addAndStartTask("xxl-job-irrigation", xxlJobConfig.getAdminAddresses(), jobInfo);
        }catch (Exception e){
            e.printStackTrace();
        }



    }


}